import 'package:flutter/material.dart';
import 'package:flutter_app/controller/MenuController.dart';
import 'package:flutter_app/module/CommonUtils.dart';
import 'package:flutter_app/net/FundroidNet.dart';
import 'package:flutter_app/page/ToastWidget.dart';
import 'package:get/get.dart';

import 'HeadIconWidget.dart';
import 'HeadTitleWidget.dart';

class HeadWidget extends StatefulWidget {
  @override
  _HeadState createState() => _HeadState();
}

//绑定注入控制器
final MenuController _menuController = Get.put(MenuController());

class _HeadState extends State<HeadWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.black,
        width: CommonUtils.getScreenWidth(context),
        child: Column(
          //主方向居中
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              padding: EdgeInsets.only(top: 20, bottom: 20),
              //最大宽度
              constraints: BoxConstraints(maxWidth: 1000),
              //填充
              width: double.infinity,
              color: Colors.black,
              child: Column(
                //包裹
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    padding: EdgeInsets.all(20),
                    // constraints: BoxConstraints(maxHeight: 1200),
                    constraints: BoxConstraints(),
                    child: buildRow(),
                  ),
                  SizedBox(
                    height: 40,
                  ),
                  Text(
                    "一个爱喝茶的程序员",
                    style: TextStyle(
                        fontSize: 22,
                        fontWeight: FontWeight.w600,
                        color: Colors.white),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 20, right: 20),
                    child: Text(
                      "2021年是一个不平凡的年代，作为新一代码农，我们需要砥砺前行，在每一个下雨天",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w400,
                        color: Colors.white,
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 40,
                  ),
                  TextButton(
                    //查看更多点击事件
                    onPressed: () {
                      Toast.toast(context, msg: "查看更多");
                    },
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          "查看更多",
                          style: TextStyle(color: Colors.white),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Icon(Icons.arrow_forward_rounded),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 40,
                  ),
                ],
              ),
            ),
          ],
        ));
  }

  //第一行标题
  Widget buildRow() {
    return Row(
      children: [
        buildLeftMenu(),
        Text(
          "早起的年轻人",
          style: TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 22,
            color: Colors.red,
          ),
        ),
        Expanded(child: buildMiddleRow()),
        buildRightImage(),
        ElevatedButton(
            onPressed: () {
              Toast.toast(context, msg: " 联系我们");
            },
            child: Text("联系我们"))
      ],
    );
  }

  //中间的分类
  Widget buildMiddleRow() {
    //显示区域宽度小于800时隐藏顶部导航
    if (CommonUtils.getScreenWidth(context) < 840) {
      return Container();
    }
    //观察者
    return Obx(() {
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // HeadTitleWidget("首页"),
          // HeadTitleWidget("Android"),
          // HeadTitleWidget("Flutter"),
          // HeadTitleWidget("java"),
          // HeadTitleWidget("前端"),
          // HeadTitleWidget("关于我们"),

          ...List.generate(
            _menuController.menuList.length,
            (index) => HeadTitleWidget(
              _menuController.menuList[index],
              _menuController.currentSelectedIndex == index,
              onTab: () async {
                _menuController.currentSelectedIndex = index;
                if (index == 2) {
                  try {
                    var result = await FunAndroidNet(context).getHomeData(0);
                    printInfo(info: "Android文章${result.toString()}");
                  } catch (e) {
                    printError(info: e.toString());
                  }
                } else {
                  printInfo(info: "当前分类${_menuController.menuList[index]}");
                }

                // Toast.toast(context, msg: _menuController.menuList[index]);
              },
            ),
          )
        ],
      );
    });
  }

  ///左侧导航栏
  Widget buildLeftMenu() {
    if (CommonUtils.getScreenWidth(context) >= 840) {
      return Container();
    }
    return IconButton(
      icon: Icon(Icons.menu),
      color: Colors.white,
      onPressed: () {
        _menuController.homeScaffoldKey.currentState.openDrawer();
      },
    );
  }

  //右边图片按钮
  Widget buildRightImage() {
    if (CommonUtils.getScreenWidth(context) <= 580) {
      return Container();
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        HeadIconWidget(
          path: "assets/images/qq.png",
          onTab: () {
            Toast.toast(context, msg: "QQ登录");
          },
        ),
        HeadIconWidget(
          path: "assets/images/wechat.png",
          onTab: () {
            Toast.toast(context, msg: "微信登录");
          },
        ),
      ],
    );
  }
}
